﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using WinFormAssigment.Entities;
using WinFormAssigment.DAO;

namespace WinFormAssigment.BUS
{
    class ClassControl
    {
        public static List<Class> GetAllClass() {
            List<Class> list = new List<Class>();
            string sp = Query.getAllClass;
            DataTable table = DatabaseInteraction.GetSelectTable(sp);
            foreach (DataRow row in table.Rows)
            {
                string classID = row["ClassID"].ToString();
                string teacherID = row["TeacherID"].ToString();
                string className = row["ClassName"].ToString();
                DateTime timeStart = Convert.ToDateTime(row["TimeStart"].ToString());
                DateTime timeEnd = Convert.ToDateTime(row["TimeEnd"].ToString());
                string timeSlot = row["TimeSlot"].ToString();
                string room = row["Room"].ToString();
                Class c = new Class()
                {
                    ClassID = classID,
                    TeacherID = teacherID,
                    Name = className,
                    TimeStart = timeStart,
                    TimeEnd = timeEnd,
                    TimeSlot = timeSlot,
                    Room = room
                };
                list.Add(c);
            }
            return list;
        }
        public static DataTable GetClassInformation() {
            string sp = Query.getClassInformation;
            DataTable table = DatabaseInteraction.GetSelectTable(sp);
            return table;        
        }
        public static List<string> GetAllClassID() {
            List<string> list = new List<string>();
            DataTable table = AttendanceManager.GetAllClassID();
            foreach (DataRow row in table.Rows)
            {
                string classID = row["ClassID"].ToString();
                list.Add(classID);
            }
            return list;
        }
        public static List<string> GetClassIDLearning() {
            List<string> list = new List<string>();
            string sp = Query.getClassIDLearning;
            DataTable table = DatabaseInteraction.GetSelectTable(sp);
            foreach (DataRow row in table.Rows)
            {
                string classID = row["ClassID"].ToString();
                list.Add(classID);
            }
            return list;
        }
        public static List<string> GetClassIDWaiting()
        {
            List<string> list = new List<string>();
            string sp = Query.getClassIDWaiting;
            DataTable table = DatabaseInteraction.GetSelectTable(sp);
            foreach (DataRow row in table.Rows)
            {
                string classID = row["ClassID"].ToString();
                list.Add(classID);
            }
            return list;
        }
        public static List<string> GetClassIDFinished()
        {
            List<string> list = new List<string>();
            string sp = Query.getClassIDFinished;
            DataTable table = DatabaseInteraction.GetSelectTable(sp);
            foreach (DataRow row in table.Rows)
            {
                string classID = row["ClassID"].ToString();
                list.Add(classID);
            }
            return list;
        }
        public static DataTable GetClassHistoryByClassID(string classID) {
            string sp = Query.getClassHistoryByClassID;
            SqlParameter[] sqlParameter = Query.GetClassHistoryByClassID(classID);
            DataTable table = DatabaseInteraction.GetSelectTable(sp,sqlParameter);
            return table;
        }
        public static List<string> GetClassTimeStartAndTimeEnd(string classID) {
            List<string> list = new List<string>();
            string sp = Query.getClassTimeStartAndTimeEnd;
            SqlParameter[] sqlParameter = Query.GetClassTimeStartAndTimeEnd(classID);            
            DataTable table = DatabaseInteraction.GetSelectTable(sp, sqlParameter);
            if (table.Rows.Count > 0) {
                string start = Convert.ToString(table.Rows[0]["TimeStart"]);
                string end = table.Rows[0]["TimeEnd"].ToString();
                list.Add(start);
                list.Add(end);
            }
            return list;
        }
        public static DataTable GetClassDetailStudent(string classID) {
            string sp = Query.getClassDetailStudent;
            SqlParameter[] sqlParameter = Query.GetClassDetailStudent(classID);
            DataTable table = DatabaseInteraction.GetSelectTable(sp, sqlParameter);
            return table;
        }
        public static Class GetClassByClassID(string classID) {
            string sp = Query.getClassByClassID;            
            SqlParameter[] sqlParameter = Query.GetClassByClassID(classID);
            DataTable table = DatabaseInteraction.GetSelectTable(sp, sqlParameter);
            if (table.Rows.Count > 0)
            {
                string ID = table.Rows[0]["ClassID"].ToString();
                string teacherID = table.Rows[0]["TeacherID"].ToString();
                string className = table.Rows[0]["ClassName"].ToString();
                DateTime timeStart = Convert.ToDateTime(table.Rows[0]["TimeStart"].ToString());
                DateTime timeEnd = Convert.ToDateTime(table.Rows[0]["TimeEnd"].ToString());
                string timeSlot = table.Rows[0]["TimeSlot"].ToString();
                string room = table.Rows[0]["Room"].ToString();
                Class c = new Class()
                {
                    ClassID = ID,
                    TeacherID = teacherID,
                    Name = className,
                    TimeStart = timeStart,
                    TimeEnd = timeEnd,
                    TimeSlot = timeSlot,
                    Room = room
                };
                return c;
            }
            else
            {
                return null;
            }
        }
        public static bool InsertClass(Class c) {
            string sp = Query.insertClass;
            SqlParameter[] sqlParameter = Query.InsertClass(c);
            bool b = DatabaseInteraction.InteractionData(sp,sqlParameter);
            return b;
        }
        public static bool UpdateClass(Class c) {
            string sp = Query.updateClass;
            SqlParameter[] sqlParameter = Query.UpdateClass(c);
            bool b = DatabaseInteraction.InteractionData(sp,sqlParameter);
            return b;
        }
        public static bool UpdateClassNewTeacher(Class c, DateTime timeLine) {
            string sp = Query.updateClassNewTeacher;
            SqlParameter[] sqlParameter = Query.UpdateClassNewTeacher(c,timeLine);
            bool b = DatabaseInteraction.InteractionData(sp, sqlParameter);
            return b;
        }
        public static bool UpdateClassHistoryNewTeacher(ClassHistory c) {
            string sp = Query.updateClassHistoryNewTeacher;
            SqlParameter[] sqlParameter = Query.UpdateClassHistoryNewTeacher(c);
            bool b = DatabaseInteraction.InteractionData(sp,sqlParameter);
            return b;
        }
        public static bool InsertClassHistoryNewTeacher(ClassHistory c) {
            string sp = Query.insertClassHistoryNewTeacher;
            SqlParameter[] sqlParameter = Query.InsertClassHistoryNewTeacher(c);
            bool b = DatabaseInteraction.InteractionData(sp,sqlParameter);
            return b;
        }
        public static bool InsertClassHistory(ClassHistory c) {
            string sp = Query.insertClassHistory;
            SqlParameter[] sqlParameter = Query.InsertClassHistory(c);
            bool b = DatabaseInteraction.InteractionData(sp,sqlParameter);
            return b;
        }
        public static DataTable SearchClassByClassID(string classID) {            
            string sp = Query.searchClassByClassID;
            SqlParameter[] sqlParameter = Query.SearchClassByClassID(classID);
            DataTable table = DatabaseInteraction.GetSelectTable(sp,sqlParameter);
            return table;
        }
    }
}
